package com.tuteng.administrator.ashospital.comprehensive;

import android.content.Context;
import android.widget.EditText;
import android.widget.Toast;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2016/2/5.
 */
public class checkutils {


    /**
     * 检查并Toast提示错误的密码长度
     * @param password
     * @param name
     * @return
     */
    public static boolean checkLength(EditText password, String name) {
        boolean ret = true;
        String toastString = "";
        Context context = password.getContext();
        int minLength = 6;
        int maxLength =12;
        String passwordString = password.getText().toString();
        if (passwordString.isEmpty()) {
            toastString = "不能为空";
            ret = false;
        } else if (passwordString.length() < minLength) {
            toastString = String.format("不能少于%d位", minLength);
            ret = false;
        } else if (passwordString.length() > maxLength) {
            toastString = String.format("不能多于%d位", maxLength);
            ret = false;
        }
        if (!ret) {
            Toast.makeText(context, name + toastString, Toast.LENGTH_SHORT).show();
            password.requestFocus();
        }
        return ret;
    }

    /****
     * 判断身份证号是否是18位
     * @param idcard
     * @para
     * @return
     */
    public static boolean checkLengthofidcard(EditText idcard) {
        boolean ret = true;
        String toastString = "";
        Context context = idcard.getContext();
        int Length = 18;

        String passwordString = idcard.getText().toString();
        if (passwordString.isEmpty()) {
            toastString = "身份证不能为空";
            ret = false;
        } else if (passwordString.length() !=Length) {
            toastString = String.format("不能少于%d位", Length);
            ret = false;
        }
        if (!ret) {
            Toast.makeText(context, toastString, Toast.LENGTH_SHORT).show();
            idcard.requestFocus();
        }
        return ret;
    }

    /**
     * 判断电话号码是否为11位
     * @param phone
     * @param
     * @return
     */
    public static boolean checkLengthofphone(EditText phone) {
        boolean ret = true;
        String toastString = "";
        Context context = phone.getContext();
        int Length = 11;

        String passwordString = phone.getText().toString();
        if (passwordString.isEmpty()) {
            toastString = "手机号码不能为空";
            ret = false;
        } else if (passwordString.length() !=Length) {
            toastString = String.format("手机号码填写错误请填写11位手机号码");
            ret = false;
        }
        if (!ret) {
            Toast.makeText(context, toastString, Toast.LENGTH_SHORT).show();
            phone.requestFocus();
        }
        return ret;
    }

    /**
     * 判断输入框内容是否为空
     * @param text
     * @param s
     * @return
     */
    public static boolean CheckIsemptyEditText( EditText text,String s) {
        boolean ret = true;
        String toastString = "";
        Context context = text.getContext();


        String passwordString = text.getText().toString();
        if (passwordString.isEmpty()) {
            toastString = s+"不能为空";
            ret = false;
        }
        if (!ret) {
            Toast.makeText(context, toastString, Toast.LENGTH_SHORT).show();
            text.requestFocus();
        }
        return ret;
    }
    /**

     * 根据身份证号码计算出生日期

     * @param idCardNo

     * @return

     */

    public static String IDCardNoToBirthday(String idCardNo){

        if(idCardNo.length() != 18 ){

            return null;

        }

//通过String[]的subString方法来读取信息

        String yyyy = idCardNo.substring(6, 10);//出生的年份

        String mm = idCardNo.substring(10,12);

        String dd = idCardNo.substring(12,14);

        String birth = yyyy.concat("-").concat(mm).concat("-").concat(dd);

        Date date = new Date();

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

        Date birthday = null;

        try {

            birthday = df.parse(birth);

        } catch (ParseException e) {

// TODO Auto-generated catch block

            e.printStackTrace();

        }
        SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd 00:00.000");
        String tablename=dateFormat.format( birthday);
        return tablename;

    }
    /**
     * @param checkType 校验类型：0校验手机号码，1校验座机号码，2两者都校验满足其一就可
     * @param phoneNum
     * */
    public static boolean validPhoneNum(String checkType,String phoneNum){
        boolean flag=false;
        Pattern p1 = null;
        Pattern p2 = null;
        Matcher m = null;
        p1 = Pattern.compile("^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\\d{8})?$");
        p2 = Pattern.compile("^(0[0-9]{2,3}\\-)?([1-9][0-9]{6,7})$");
        if("0".equals(checkType)){
            System.out.println(phoneNum.length());
            if(phoneNum.length()!=11){
                return false;
            }else{
                m = p1.matcher(phoneNum);
                flag = m.matches();
            }
        }else if("1".equals(checkType)){
            if(phoneNum.length()<11||phoneNum.length()>=16){
                return false;
            }else{
                m = p2.matcher(phoneNum);
                flag = m.matches();
            }
        }else if("2".equals(checkType)){
            if(!((phoneNum.length() == 11 && p1.matcher(phoneNum).matches())||(phoneNum.length()<16&&p2.matcher(phoneNum).matches()))){
                return false;
            }else{
                flag = true;
            }
        }
        return flag;
    }

}
